package com.fg.api;



import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.jets3t.service.S3Service;
import org.jets3t.service.StorageService;
import org.jets3t.service.acl.AccessControlList;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.model.StorageObject;
import org.jets3t.service.multi.DownloadPackage;
import org.jets3t.service.multi.SimpleThreadedStorageService;
import org.jets3t.service.security.AWSCredentials;

import com.fg.model.FileInfo;
import com.fg.model.ImageVO;
import com.fg.security.LocalEncrypter;


public class AwsS3Controller {
	private String awsAccessKey;
	private String awsSecretKey;
	private String upload_bucket;
	private AWSCredentials awsCredentials;
	
	public AwsS3Controller() throws Exception{
		awsAccessKey = getKeyString_inProperties("properties/AmazonAccessInfo.properties", "access_id");
		awsSecretKey = getKeyString_inProperties("properties/AmazonAccessInfo.properties", "secret_access_key");
		upload_bucket = getKeyString_inProperties("properties/AmazonAccessInfo.properties", "upload_bucket");
		awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);
	}
	
	/**
	 * 이미지 업로드 
	 * @param image_object Aws S3 upload form 에 맞는 가공된 이미지 객체 
	 * @param path  버켓안에 이미지가 저장될 path 버켓이름/xxxx/xxxx/xxxx/
	 * @throws Exception
	 */
	public void uploadFile(List<AwsFileObject> image_object, String bucket_path) throws Exception{
		StorageService ss = new RestS3Service(awsCredentials);
		StorageObject[] fileObject = new StorageObject[image_object.size()];
		for(int i = 0 ; i < image_object.size() ; i++){
			AwsFileObject object = image_object.get(i);
			fileObject[i] = new StorageObject(object.getFile());
			fileObject[i].setName(object.getFile_name());
			fileObject[i].setContentType(object.getContent_type());
			fileObject[i].setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
		}
		
		SimpleThreadedStorageService stss = new SimpleThreadedStorageService(ss);
		stss.putObjects(bucket_path, fileObject);
	}
	
	public void uplaodFile(AwsFileObject file, String bucket_path) throws Exception{
		S3Service s3s = new RestS3Service(awsCredentials);
		S3Bucket file_bucket =  s3s.getBucket(upload_bucket);
		S3Object fileObject  = new S3Object(file.getFile());
		fileObject.setName(bucket_path+file.getFile_name());
		fileObject.setContentType(file.getContent_type());
		fileObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
		s3s.putObject(file_bucket, fileObject);
	}
	
//	public void uplaodImage(AwsFileObject file, String bucket_path) throws Exception{
//		S3Service s3s = new RestS3Service(awsCredentials);
//		S3Object fileObject  = new S3Object(file.getFile());
//		fileObject.setName(file.getFile_name());
//		fileObject.setContentType(file.getContent_type());
//		fileObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
//		s3s.putObject(bucket_path, fileObject);
//	}
	
	public void deleteFiles(List<ImageVO> image_list) throws Exception{
		StorageService ss = new RestS3Service(awsCredentials);
		
		StorageObject[] file_object = new StorageObject[image_list.size()];
		for(int i=0; i < image_list.size() ; i++){
			file_object[i] = ss.getObject(upload_bucket, "images/"+image_list.get(i).getImage_path());
		}
		SimpleThreadedStorageService sts = new SimpleThreadedStorageService(ss);
		sts.deleteObjects(upload_bucket, file_object);
	}
	
	public void deleteFile(ImageVO image) throws Exception{
		StorageService ss = new RestS3Service(awsCredentials);
		
		StorageObject file_object = new StorageObject();
		file_object = ss.getObject(upload_bucket, "images/"+image.getImage_path());
		ss.deleteObject(upload_bucket, file_object.getKey());
	}
	
	public String getKeyString_inProperties(String propertiesName, String key) throws Exception {
		InputStream in = LocalEncrypter.class.getClassLoader().getResourceAsStream(propertiesName);
		Properties properties = new Properties();
		properties.load(in);
		return properties.getProperty(key);
	}
	
	
}
